﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FreedomX2.Core.Transport.SocketProvider
{
    public class SocketCenter
    {
        private SocketServer socketServer;
        public delegate void dReceiveFinish(System.Net.Sockets.Socket socket, ThreadContent threadContent);
        public event dReceiveFinish ReceiveFinish;

        public void Open(string ip, int port)
        {
            socketServer = new SocketServer(ip, port);
            SocketThread.ThreadFinish += new SocketThread.DeleThreadFinish(SocketThread_ThreadFinish);
            socketServer.ServerStart();
        }

        void SocketThread_ThreadFinish(System.Net.Sockets.Socket socket, SocketThread socketThread)
        {
            if (ReceiveFinish != null)
            {
                ReceiveFinish.Invoke(socket, SocketBlocks.GetThreadContent(socketThread));
            }
        }
        public void Send(string ip, int port, string Message)
        {
            SocketClient sc = new SocketClient(ip, port);
            sc.SendMessage(Message);
        }
        public void Send(string ip, int port, byte[] Message)
        {
            SocketClient sc = new SocketClient(ip, port);
            sc.SendBytes(Message);
        }
        public void Send(System.Net.Sockets.Socket socket, string Message)
        {
            SocketClient.SendMessage(socket, Message);
        }
        public void Send(System.Net.Sockets.Socket socket, byte[] Message)
        {
            SocketClient.SendBytes(socket, Message);
        }
        public void Close()
        {
            socketServer.ServerStop();

        }
    }
}
